<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Withdrawals');
Globals::requireTable('Msg');
Globals::requireTable('Account');
Globals::requireTable('Vmoney');
Globals::requireTable('Vlevel');
Globals::requireTable('Acappreciate');
Globals::requireTable('Circle');

class UserController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $WithdrawalsTable;
	protected $MsgTable;
	protected $AccountTable;
	protected $VmoneyTable;
	protected $VlevelTable;
	protected $AcappreciateTable;
	protected $CircleTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable          = new UserTable($config);
		$this->ProductTable       = new ProductTable($config);
		$this->OrderTable         = new OrderTable($config);
		$this->WithdrawalsTable   = new WithdrawalsTable($config);
		$this->MsgTable           = new MsgTable($config);
		$this->AccountTable       = new AccountTable($config);
		$this->VmoneyTable        = new VmoneyTable($config);
		$this->VlevelTable        = new VlevelTable($config);
		$this->AcappreciateTable  = new AcappreciateTable($config);
		$this->CircleTable        = new CircleTable($config);
		dolog(json_encode($_GET));
	}
	
	/**
	 * 首页数据
	 */
	public function indexAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'orders'=>0,'messages'=>0,'accounts'=>0));
	        exit();
	    }
	    
	    $orders = $this->OrderTable->listCount(array('uid'=>$uid,'status>?'=>0,'type'=>1));
	    $messages = $this->MsgTable->listCount(array('uid'=>$uid,'status'=>0));
	    $accounts = $this->AccountTable->listCount(array('uid'=>$uid));
	    echo json_encode(array('returncode'=>0,'orders'=>(string)$orders,'messages'=>(string)$messages,'accounts'=>(string)$accounts));
	    exit();
	}
	
	/**
	 * 购买记录
	 * uid/token&machine_type
	 */
	public function orderlistAction()
	{
        $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array()));
	        exit();
	    }
	    
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 0 : ($page - 1);
	    $size = $this->getIntParam('size');
	    $size = $size < 1 ? 999 : $size;
	    $pv = $page * $size;
	    
	    $time = time();
	    $result = array();
	    $user = $this->UserTable->getRow($uid);
	    $orders = $this->OrderTable->listAll(array('uid'=>$uid,'status>?'=>0,'type'=>1),'id desc',$size,$pv);
	    foreach ($orders as $v){
	        $product = $this->ProductTable->getRow($v['pid']);
	        $account = $this->AccountTable->getRow($v['aid']);
	        $tmp['orderid'] = (string)$v['orderid'];
	        $tmp['name'] = (string)$product['name'].$v['days'].'天试用期';
	        $tmp['money'] = (string)$v['money'];
	        //$tmp['wechat'] = (string)$user['wechatID'];
	        $tmp['wechat'] = 'xiaoshulinapp';
	        $tmp['dayend'] = $v['endtime'] < $time ? '已过期' : (string)date('Y-m-d',$v['endtime']);
	        //$tmp['username'] = (string)$account['username'];
	        //$tmp['password'] = (string)$account['password'];
	        $tmp['username'] = $v['endtime'] < $time ? '******' : (string)$account['username'];
	        $tmp['password'] = $v['endtime'] < $time ? '******' : (string)$account['password'];
	        $tmp['img'] = (string)DOMAIN.str_replace('../', '', $product['img']);
	        $tmp['pushuid'] = $v['uid_from'] == 1 ? $v['o_uid_from'] : $v['uid_from'];
	        $tmp['pushwechat'] = $account['wechat'];
	        $result[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	/**
	 * 注册
	 */
	public function signAction(){
	    $token = $this->getParam('token');
	    $machine_type= $this->getIntParam('machine_type');
	    $machine = $this->getParam('machine');
	    if(empty($token)){
	        echo json_encode(array('returncode'=>-1,'uid'=>'0','sign'=>'','uidtoken'=>''));
	        //echo json_encode(array('returncode'=>-1,'uid'=>'0','sign'=>''));
	        exit();
	    }
	    
	    $check_sign_1 = 0;
	    $check_sign_2 = 0;
	    
	    $str_1 = substr($token, -7);
        $new_num_arr = array();
	    for ($ii = 0 ; $ii < strlen($str_1) ; $ii++){
	        $tmp_num = substr($str_1, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $str_2 = '';
	    foreach ($new_num_arr as $v){
	        $str_2 .= (string)$v;
	    }
	    $str_2 = $str_2 + 6387612;
	    $str_2 = substr($str_2, 1);
	    $new_num_1 = substr($str_2, 0,-2);
	    $new_num_2 = substr($str_2, -1);
	    
	    $machine_1 = substr($machine, 0,-3);
	    $machine_2 = substr($machine, -1);
	    
	    if($new_num_1 == $machine_1 && $new_num_2 == $machine_2){
	        $check_sign_1 = 1;
	    }
	    
	    
	    
	    
	    
	    $sign1 = substr($token, 0,12);
	    $new_num_arr = array();
	    for ($ii = 0 ; $ii < strlen($sign1) ; $ii++){
	        $tmp_num = substr($sign1, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $sign2 = '';
	    foreach ($new_num_arr as $v){
	        $sign2 .= (string)$v;
	    }
	    $sign3 = $sign2 + $machine + 832578164963;
	    
	    $register = $this->getParam('register');
	    $new_num_arr = array();
	    for ($ii = 0 ; $ii < strlen($register) ; $ii++){
	        if($ii == 3 || $ii == (strlen($register)-3)){
	            continue;
	        }else{
	            $tmp_num = substr($register, $ii , 1);
	            $new_num_arr[] = $tmp_num;
	        }
	    }
	    $sign4 = '';
	    foreach ($new_num_arr as $v){
	        $sign4 .= (string)$v;
	    }
	    
	    if($sign4 == $sign3){
	        $check_sign_2 = 1;
	    }
	    
	    
	    if($check_sign_1 == 1 || $check_sign_2 == 1){
	        $user = $this->UserTable->getRow("token = '{$token}'");
	        if($user['id'] < 1){
	            $data['nickname'] = substr($token, 3,6).rand(1000, 9999);
	            $data['username'] = substr($token, 3,6).rand(1000, 9999);
	            $data['token'] = $token;
	            $data['machine_type'] = (int)$machine_type;
	            $data['reg_ip'] = $_SERVER["REMOTE_ADDR"];
	            $user['id'] = $this->UserTable->add($data,true);
	            
	            $msgdata['uid'] = $user['id'];
	            $msgdata['createdate'] = date('Y-m-d H:i:s');
	            $msgdata['msg'] = '我们未曾相识！此日相见！不恩求与你青葱岁月卧看晨云，不奢望与你肤斑白发坐赏余晖，不妄想与你对碑残刻化泥沉眠！只但愿百无聊赖、夕沉孤寂时心念有我！';
	            $msgdata['status'] = 0;
	            $this->MsgTable->add($msgdata);
	             
	            $vlevel_data['uid'] = $user['id'];
	            $vlevel_data['type'] = 5;
	            $vlevel_data['level'] = 10;
	            $vlevel_data['remark'] = '新用户注册成功获得10v级';
	            $this->VlevelTable->add($vlevel_data);
	        }else{
	            //echo json_encode(array('returncode'=>-2,'uid'=>'0','sign'=>'','uidtoken'=>''));
	            //echo json_encode(array('returncode'=>0,'uid'=>$user['id'],'sign'=>''));
	            //exit();
	        }
	        
	        
	        
	        $re_num_arr = array();
	        for ($ii = 0 ; $ii < strlen($user['id']) ; $ii++){
	            $tmp_num = substr($user['id'], $ii , 1);
	            $re_num_arr[] = $tmp_num;
	        }
	        krsort($re_num_arr);
	        $re_num_str = '';
	        foreach ($re_num_arr as $v){
	            $re_num_str .= (string)$v;
	        }
	        $re_num_str += 3851947;
	        $re_num_arr = array();
	        for ($ii = 0 ; $ii < strlen($re_num_str) ; $ii++){
	            $tmp_num = substr($re_num_str, $ii , 1);
	            if($ii == 1 || $ii == (strlen($re_num_str)-1)){
	                $re_num_arr[] = rand(0, 9);
	            }
	            $re_num_arr[] = $tmp_num;
	        }
	        $re_str = '';
	        foreach ($re_num_arr as $v){
	            $re_str .= (string)$v;
	        }
	        
	        
	        $uidtoken = getUidToken($user['id']);
	        
	        
	        echo json_encode(array('returncode'=>0,'uid'=>(string)$user['id'],'sign'=>(string)$re_str,'uidtoken'=>$uidtoken));
	        //echo json_encode(array('returncode'=>0,'uid'=>(string)$user['id'],'sign'=>(string)$re_str));
	        exit();
	    }else{
	        echo json_encode(array('returncode'=>-999,'uid'=>'0','sign'=>'','uidtoken'=>''));
	        //echo json_encode(array('returncode'=>-999,'uid'=>'0','sign'=>''));
	        exit();
	    }
	}
	
	/**
	 * 申请提现页面
	 */
	public function withdrawalsAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'money'=>'0'));
	        exit();
	    }
	    
	    $money = 0;
	    $orders = $this->OrderTable->listAll(array('uid'=>$uid,'status'=>2,'type'=>1));
	    foreach ($orders as $v){
	        $money += $v['money'];
	    }
	    echo json_encode(array('returncode'=>0,'money'=>(string)$money));
	    exit();
	}
	
	/**
	 * 申请提现功能
	 */
	public function dowithdrawalsAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1));
	        exit();
	    }
	    
	    $alipay = $this->getParam('alipay');
	    if(empty($alipay)){
	        echo json_encode(array('returncode'=>-2));
	        exit();
	    }
	    
	    $this->UserTable->update(array('alipay'=>$alipay),$uid);
	    
	    $money = 0;
	    $orderids = array();
	    $orders = $this->OrderTable->listAll(array('uid_from'=>$uid,'status'=>2,'type'=>1));
		//var_dump($orders);
        if(count($orders) < 1){
	        echo json_encode(array('returncode'=>-3));
	        exit();
	    }
	    
	    foreach ($orders as $v){
	        $money += $v['money'];
	        $orderids[] = $v['orderid'];
	        $this->OrderTable->update(array('status'=>3),$v['id']);
	    }
	    
	    $data['uid'] = $uid;
	    $data['money'] = $money;
	    $data['createdate'] = date('Y-m-d H:i:s');
	    $data['status'] = 0;
	    $data['orderids'] = implode(',', $orderids);
	    $data['alipay'] = $alipay;
	    $this->WithdrawalsTable->add($data);
	    
	    $exreas = array(
	        'pushtype' => 25,
	        'content' => '请在两天内给出通过或拒绝等审批结果'
	    );
	    do_jpush(1374, '有用户申请提现请尽快审批', $exreas);
	    
	    echo json_encode(array('returncode'=>0));
	    exit();
	}
	
	/**
	 * v级列表
	 */
	public function vlistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array(),'vcount'=>'0'));
	        exit();
	    }
	    
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 0 : ($page - 1);
	    $size = $this->getIntParam('size');
	    $size = $size < 1 ? 999 : $size;
	    $pv = $page * $size;
	    
	    $list = array();
	    $vcount = 0;
	    $data = $this->VlevelTable->listAll(array('uid'=>$uid),'id desc');
	    foreach ($data as $k=>$v){
	        if($k >= $pv && $k < ($pv+$size)){
	        $tmp['name'] = (string)$v['remark'];
	        $tmp['v'] = $v['level'] > 0 ? (string)('+'.$v['level']) : $v['level'];
	        $list[] = $tmp;
	        }
	        $vcount += $v['level'];
	    }
	    /*
	    $orders = $this->OrderTable->listAll(array('uid_from'=>$uid,'status>?'=>0));
	    foreach ($orders as $v){
	        $listvalue[$v['pid']] += $v['money'];
	    }
	    foreach ($listvalue as $k=>$v){
	        $product = $this->ProductTable->getRow($k);
	        $tmp['name'] = (string)$product['name'];
	        $tmp['v'] = (string)$v;
	        $list[] = $tmp;
	        $vcount += $v;
	    }
	    */
	    echo json_encode(array('returncode'=>0,'list'=>$list,'vcount'=>(string)$vcount));
	    exit();
	}
	
	/**
	 * v币列表
	 */
	public function vmoneylistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>-1,'list'=>array(),'vmoneycount'=>'0','rechargeurl'=>RECHARGE_URL));
	        exit();
	    }
	    
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 0 : ($page - 1);
	    $size = $this->getIntParam('size');
	    $size = $size < 1 ? 999 : $size;
	    $pv = $page * $size;
	     
	    $list = array();
	    $listvalue = array();
	    $vcount = 0;
	    $listvalue = $this->VmoneyTable->listAll(array('uid'=>$uid),'id desc');
	    foreach ($listvalue as $k=>$v){
	        if($k >= $pv && $k < ($pv + $size)){
	        $tmp['name'] = (string)$v['remark'];
	        $tmp['v'] = $v['money'] > 0 ? (string)('+'.$v['money']) : $v['money'];
	        $list[] = $tmp;
	        }
	        $vcount += $v['money'];
	    }
	    $vcount = round($vcount,2);
	    echo json_encode(array('returncode'=>0,'list'=>$list,'vmoneycount'=>(string)$vcount,'rechargeurl'=>RECHARGE_URL));
	    exit();
	}
	
	/**
	 * 收入列表
	 */
    public function incomeAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'list'=>array(),'money'=>'0'));
            exit();
        }
        
        $page = $this->getIntParam('page');
        $page = $page < 1 ? 0 : ($page - 1);
        $size = $this->getIntParam('size');
        $size = $size < 1 ? 999 : $size;
        $pv = $page * $size;
        
        $list = array();
        $money = 0;
        //$orders = $this->OrderTable->listAll(array('uid_from'=>$uid,'status>?'=>0));
        $orders = $this->OrderTable->listAll("uid_from = '{$uid}' and status > 0 and status <> 4 and type = 1",'id desc');
        foreach ($orders as $k=>$v){
            if($k >= $pv && $k < ($pv + $size)){
            $account = $this->AccountTable->getRow($v['aid']);
            $product = $this->ProductTable->getRow($account['pid']);
            $tmp['name'] = (string)$account['username'].'转让，'.$product['name'];
            $tmp['money'] = (string)$v['money'];
            $list[] = $tmp;
            }
            $money += $v['money'];
        }
        echo json_encode(array('returncode'=>0,'list'=>$list,'money'=>(string)$money));
        exit();
    }
	
	/**
	 * 用户中心
	 */
    public function ucenterAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'v'=>'0','money'=>'0','inmoney'=>'0','income'=>'0','username'=>'','v-money'=>'','alipay'=>'','usercount'=>0));
            exit();
        }
        
        $money = $income = 0;
        //$orders = $this->OrderTable->listAll(array('uid_from'=>$uid,'status>?'=>0));
        $orders = $this->OrderTable->listAll("uid_from = '{$uid}' and status > 0 and type = 1");
        foreach ($orders as $v){
			$vlevel += $v['money'];
			
			if($v['status'] != 4){
				$income += $v['money'];
			}
            
            if($v['status'] == 2){
                $money += $v['money'];
            }
        }
        
        $vcount = 0;
        $listvalue = $this->VmoneyTable->listAll(array('uid'=>$uid));
        foreach ($listvalue as $k=>$v){
            $vcount += $v['money'];
        }
        
        $vlevel = 0;
        $listvalue = $this->VlevelTable->listAll(array('uid'=>$uid));
        foreach ($listvalue as $k=>$v){
            $vlevel += $v['level'];
        }
        
        $usercount = time() / 10000;
        $usercount = (int)$usercount;
        $usercount -= 130000;
        
        $inmoney = 0;
        $db = $this->UserTable->getDatabase();
        $sql = "select SUM(price) as sprice from tb_circle_order where old_cid in (select id from tb_circle where uid = {$uid}) and status = 2 ";
        $db->query($sql);
        $re = $db->getList();
        if(is_array($re)){
            foreach ($re as $v){
                $inmoney = $v['sprice'];
            }
        }
        
        $messages = $this->MsgTable->listCount(array('uid'=>$uid,'status'=>0));
        
        $user = $this->UserTable->getRow($uid);
        echo json_encode(array(
            'returncode'=>0,
            'v'=>(string)round($vlevel,2),
            'money'=>(string)round($money,2),
            'inmoney'=>(string)round($inmoney,2),
            'income'=>(string)round($income,2),
            'username'=>(string)$user['nickname'],
            'v-money'=>(string)round($vcount,2),
            'alipay'=>(string)$user['alipay'],
            'usercount'=>$usercount,
            'message'=>(string)$messages
        ));
        exit();
    }
	
	/**
	 * 消息列表
	 */
    public function msglistAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'list'=>array()));
            exit();
        }
        
        $page = $this->getIntParam('page');
        $page = $page < 1 ? 0 : ($page - 1);
        $size = $this->getIntParam('size');
        $size = $size < 1 ? 999 : $size;
        $pv = $page * $size;
        
        $list = array();
        $msgs = $this->MsgTable->listAll(array('uid'=>$uid),'id DESC',$size,$pv);
        foreach ($msgs as $v){
            $tmp['message'] = (string)$v['msg'];
            $tmp['time'] = (string)$v['createdate'];
            $tmp['status'] = (string)$v['status'];
            $tmp['id'] = (string)$v['id'];
            $list[] = $tmp;
        }
        echo json_encode(array('returncode'=>0,'list'=>$list));
        exit();
    }
	
	/**
	 * 消息详情
	 */
    public function msgdetailAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'message'=>''));
            exit();
        }
        
        $msgid = $this->getIntParam('messageid');
        if($msgid == 0){
            echo json_encode(array('returncode'=>-2,'message'=>''));
            exit();
        }
        
        $message = $this->MsgTable->getRow($msgid);
        $this->MsgTable->update(array('status'=>1),$msgid);
        echo json_encode(array('returncode'=>0,'message'=>(string)$message['msg']));
        exit();
    }
	
    /**
     * 转租账号管理
     */
    public function rentaccountAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'list'=>array()));
            exit();
        }
        
        $page = $this->getIntParam('page');
        $page = $page < 1 ? 0 : ($page - 1);
        $size = $this->getIntParam('size');
        $size = $size < 1 ? 999 : $size;
        $pv = $page * $size;
        
        $list = array();
        $products = $this->ProductTable->listAll('','id desc',$size,$pv);
        foreach ($products as $v){
            $accounts = $this->AccountTable->listCount(array('uid'=>$uid,'pid'=>$v['id']));
            if($accounts > 0){
                $tmp['pid'] = (string)$v['id'];
                $tmp['count'] = (string)$accounts;
                $tmp['name'] = (string)$v['name'];
                $tmp['img'] = (string)DOMAIN.str_replace('../', '', $v['img']);
                $list[] = $tmp;
            }
        }
        
        echo json_encode(array('returncode'=>0,'list'=>$list));
        exit();
    }
    
    /**
     * 转租账号平台详情
     */
    public function rentaccountdetailAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'list'=>array()));
            exit();
        }
        
        $pid = $this->getIntParam('pid');
        if($pid == 0){
            echo json_encode(array('returncode'=>-2,'list'=>array()));
            exit();
        }
        
        $time = time();
        $product = $this->ProductTable->getRow($pid);
        $list = array();
        $accounts = $this->AccountTable->listAll(array('uid'=>$uid,'pid'=>$pid));
        foreach ($accounts as $v){
            $tmp['aid'] = (string)$v['id'];
            $tmp['name'] = (string)$v['username'];
            $tmp['img'] = (string)DOMAIN.str_replace('../', '', $product['img']);
            $tmp['endtime'] = $time > $v['endtime'] ? '已过期' : (string)date('Y-m-d',$v['endtime']);
            $list[] = $tmp;
        }
        echo json_encode(array('returncode'=>0,'list'=>$list));
        exit();
    }
    
    public function userdataAction(){
        echo json_encode(array('userdata'=>5346643));
        exit();
    }
    
    public function setheadpicAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'message'=>''));
            exit();
        }
        
        $avatarurl = $this->getParam('avatarurl');
        $this->UserTable->update(array('headurl'=>$avatarurl),$uid);
        
        echo json_encode(array('returncode'=>0,'message'=>'小主，你又漂亮了哦！'));
        exit();
        
    }
    
    public function acappreciateAction(){
        $uid = $this->getParam('uid');
        $token = $this->getParam('token');
        $machine_type = $this->getParam('machine_type');
        $usermodel = new UserModel($this->config);
        $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
        if($uid < 1){
            echo json_encode(array('returncode'=>-1,'message'=>''));
            exit();
        }
        
        $money = $this->getParam('appreciateyield');
        $sign = $this->getParam('appreciateverify');
        $detail_id = $this->getIntParam('appreciateid');
        $detail_uid = $this->getIntParam('appreciateuid');
        $type = $this->getIntParam('appreciatetype');
        
        $s1 = $detail_id.$detail_uid.$type.$money.'E1!?397zx.cASD!@#';
        $s2 = md5($s1);
        $s3 = substr($s2, 10,8);
        
        if($s3 != $sign){
            echo json_encode(array('returncode'=>-1,'message'=>'验证错误'));
            exit();
        }
        
        $user_money = $this->VmoneyTable->getUserVmoneyCount($uid);
        if($user_money < $money){
            echo json_encode(array('returncode'=>-2,'message'=>'V币不足'));
            exit();
        }
        
        switch ($type){
            case 1:
                $detail_data = $this->CircleTable->getRow($detail_id);
                if(!isset($detail_data['id']) || $detail_data['id'] < 1){
                    echo json_encode(array('returncode'=>-3,'message'=>'无此圈子'));
                    exit();
                }
                
                $vmoney_data = array(
                    'uid' => $uid,
                    'money' => 0 - $money,
                    'type' => 10,
                    'remark' => '打赏圈子，扣除'.$money
                );
                $this->VmoneyTable->add($vmoney_data);
                
                $newmoney = $money - 0.01;
                $vmoney_data = array(
                    'uid' => $detail_data['uid'],
                    'money' => $newmoney,
                    'type' => 11,
                    'remark' => '被打赏圈子，得到'.$newmoney
                );
                $this->VmoneyTable->add($vmoney_data);
                
                $exreas = array(
                    'pushtype' => 17,
                    'content' => "用户ID：{$uid} 对您发布的圈子打赏了{$newmoney}V币。",
                    'customfield' => 'http://mp.weixin.qq.com/s/***VipUserCentreViewController***'
                );
                do_jpush($detail_data['uid'],'主人来赏金了!',$exreas);
                
                break;
            case 2:
                $vmoney_data = array(
                    'uid' => $uid,
                    'money' => 0 - $money,
                    'type' => 12,
                    'remark' => '打赏官网，扣除'.$money
                );
                $this->VmoneyTable->add($vmoney_data);
                
                $newmoney = $money;
                break;
            case 3:
                $vmoney_data = array(
                    'uid' => $uid,
                    'money' => 0 - $money,
                    'type' => 13,
                    'remark' => '打赏用户，扣除'.$money
                );
                $newmoney = $money - 0.01;
                $vmoney_data = array(
                    'uid' => $detail_uid,
                    'money' => $newmoney,
                    'type' => 14,
                    'remark' => '被打赏用户，得到'.$newmoney
                );
                $this->VmoneyTable->add($vmoney_data);
                
                $exreas = array(
                    'pushtype' => 17,
                    'content' => "用户ID：{$uid} 给您退还或者打赏了{$newmoney}V币。",
                    'customfield' => 'http://mp.weixin.qq.com/s/***VipUserCentreViewController***'
                );
                do_jpush($detail_uid,'主人又来钱了!',$exreas);
                
                break;
            default:
                echo json_encode(array('returncode'=>-4,'message'=>'类型错误'));
                exit();
                break;
        }
        
        $data = array(
            'uid' => $uid,
            'type' => $type,
            'detail_id' => $detail_id,
            'createtime' => date('Y-m-d H:i:s'),
            'money' => $money,
            'detail_uid' => $detail_uid
        );
        $this->AcappreciateTable->add($data);
        
        echo json_encode(array('returncode'=>0,'message'=>"打赏了{$money}V币，土豪心意已送达"));
        exit();
    }
    
	protected function out()
	{
		parent::out();
	}
}

Config::extend('UserController', 'Controller');
